/*******************************************************************************
 * Witz - Winboard Chess Engine                                                *
 * TIMEMANAGER.C                                                               *
 *                                                                             *
 * Manages all time functions in the program                                   *
 *                                                                             *
 * Please refer to the licence notice in MAIN.C                                *
 *                                                                             *
 * Copywright (C) 2006 Pradu Kannan                                            *
 * http://witz.sf.net/                                                         *
 ******************************************************************************/

#include "build.h"
#include "defs.h"
#include "protos.h"
#include "data.h"

int timeLimit=0;
int timeLeft=0;
int timeInc=0;
int movesInitial=0;
int movesLeft=0;
int endTime=0;
int startTime=0;

/*getms()
 *gets time in milliseconds
 *@returns the number of milliseconds since the epoch
 */
int getms()
{
	struct timeb timebuffer;
	ftime(&timebuffer);
	return (timebuffer.time * 1000) + timebuffer.millitm;
}

/*startClock
 *starts the clock when on move
 *it must be called everytime on move
 */
void startClock()
{
	startTime=endTime=getms();
	if(movesInitial!=0)
		endTime+=timeLeft/(movesLeft+1);
	else if(timeLimit!=0)
		endTime+=timeLimit;
	else if(timeLeft!=0)
		endTime+=timeLeft/TIME_DIVIDER;
	else
		endTime=INF;
}

/*timesUp()
 *Checks whether the thinking time is used up
 *
 *@returns true when time is up
 */
bool timesUp()
{
	if(depthLimit==INF && getms()>endTime)
		return true;
	return false;
}
